package com.novax.ex.activity.provider.controller.inner;

import com.novax.ex.activity.infrastructure.entity.OperationV2Rule;
import com.novax.ex.activity.open.api.inner.OperationV2RuleApi;
import com.novax.ex.activity.open.model.request.OperationV2RuleRequest;
import com.novax.ex.activity.open.model.response.OperationV2RuleResponse;
import com.novax.ex.activity.provider.controller.BaseActivityController;
import com.novax.ex.activity.provider.service.OperationV2RuleService;
import com.novax.ex.common.constant.InternationalCode;
import com.novax.ex.common.results.ReturnResult;
import com.novax.ex.common.util.CopyUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * @Description 活动落地页配置V2
 * @Author ChenXi
 * @Date 2023/04/18/10:08
 */

@RestController
public class OperationV2RuleController extends BaseActivityController implements OperationV2RuleApi {

    @Resource
    private OperationV2RuleService operationV2RuleService;

    @Override
    public ReturnResult<List<OperationV2RuleResponse>> list(Long operationId) {
        return ReturnResult.success(CopyUtils.copyList(operationV2RuleService.list(operationId), OperationV2RuleResponse.class));
    }

    @Override
    public ReturnResult<?> add(String languages, OperationV2RuleRequest operationV2RuleRequest, BindingResult bindingResult) {

        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LACK_VALUE));
        }
        // 判断此活动id和语言是否已配置文案
        Long count = operationV2RuleService.queryOperationIdAndLanguage(operationV2RuleRequest.getOperationId(),
                                                                        operationV2RuleRequest.getLanguage());
        if(count >= 1){
            return ReturnResult.fail(getInternational(languages, InternationalCode.DUPLICATED_DATA));
        }

        OperationV2Rule operationV2Rule = CopyUtils.copyObject(operationV2RuleRequest, OperationV2Rule.class);

        boolean res = operationV2RuleService.addSelective(operationV2Rule);
        return res ? ReturnResult.success(getInternational(languages, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(languages, InternationalCode.OPERATION_ERROR));
    }

    @Override
    public ReturnResult<?> modify(String languages, Long id, OperationV2RuleRequest operationV2RuleRequest,
                                  BindingResult bindingResult) {

        // 缺少必填项
        if(bindingResult.hasErrors()){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LACK_VALUE));
        }

        OperationV2Rule current = operationV2RuleService.find(id);
        // 语言不可更改
        if(!current.getLanguage().equals(operationV2RuleRequest.getLanguage())){
            return ReturnResult.fail(getInternational(languages, InternationalCode.LANGUAGE_CHANG));
        }

        OperationV2Rule operationV2Rule = CopyUtils.copyObject(operationV2RuleRequest, OperationV2Rule.class);
        operationV2Rule.setId(id);
        operationV2Rule.setOperationId(null);
        operationV2Rule.setLanguage(null);
        boolean res = operationV2RuleService.modifySelective(operationV2Rule);
        return res ? ReturnResult.success(getInternational(languages, InternationalCode.OPERATION_SUCCESS))
            : ReturnResult.fail(getInternational(languages, InternationalCode.OPERATION_ERROR));
    }

}
